<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
     <meta charset="utf-8">

    <title>SLLs Utbudskatalog</title>

    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    
    <link type="text/css" rel="stylesheet" href="/assets/css/pure-min.css" />
    <link type="text/css" rel="stylesheet" href="/assets/css/pure-app.css" />
    <link type="text/css" rel="stylesheet" href="/assets/css/app.css" />
</head>
<body>
    <div class="container content pure-g">
        <!-- Top navigation -->
        <nav class="pure-u-1 pure-menu pure-menu-open pure-menu-horizontal">
            <ul class="views" data-bind="foreach: config.views">
                <li>
                    <a href="#" data-bind="text: $data.name, 
                               css: { selected: $data.id == $root.chosenViewId() },
                               click: $root.goToView"></a>
                </li>
            </ul>
        </nav>

        <!-- items grid -->
        <div class="pure-u-2-3" data-bind="with: chosenViewData" id="view">
            <h2 data-bind="text: $root.chosenView.name" class="content-subhead"></h2>
            <ul data-bind="foreach: items" class="list">
                <li data-bind="click: $root.goToItem,attr: {id: hsaID}" class="list-item">
                    <h3 data-bind="text: name"></h3>
                    <p data-bind="text: hsaID"></p>
                </li>     
            </ul>
        </div>

        <!-- Chosen item -->
        <div class="pure-u-2-3 pure-form" data-bind="with: chosenItemData" id="form">
            <div class="itemInfo edit-item">
                <h1 data-bind="text: name"></h1>
                <p><label>HSA</label>: <span data-bind="text: hsaID"></span></p>
            </div>
            <div class="itemEdit edit-item">
                <label>Name</label>
                <input data-bind="value: name">
                <button data-bind="click: $root.cancelSave" class="pure-button">Avbryt</button>
                <button data-bind="click: $root.saveItem" class="pure-button pure-button-primary">Spara</button>
            </div>
        </div>
    </div>


<script src="../assets/js/lib/knockout-3.1.0.js"></script>
<script src="../assets/js/lib/jquery-1.11.0.min.js"></script>
<script src="../assets/js/lib/sammy.js" type="text/javascript"></script>

<script type="text/javascript">
function AppVM() {
    // Data
    var self = this;

    self.config = {
        start: '#vantetid',
        views: [
            {
                name:'Väntetid',
                hash:'vantetid',
                data:'/data/vantetid.json',
                item:'/data/vantetid-',
                templates: {
                    form:'edit-vantetid',
                    view:'list-vantetid'
                }
            },
            {
                name:'Utbud',
                hash:'utbud',
                data:'/data/utbud.json',
                item:'/data/utbud-item.json',
                templates: {
                    form:'edit-vantetid',
                    view:'list-vantetid'
                }
            }
        ]
    };

    // Observables
    self.chosenViewId = ko.observableArray();
    self.chosenViewData = ko.observable();
    self.chosenItemData = ko.observable();
    self.chosenItemIndex = ko.observable();
    self.chosenItem = {};
    self.chosenView = {};

    // Behaviours    
    self.goToView = function(view) {
        self.chosenView = view;
        location.hash = view.hash
        $('#view').addClass('pure-u-2-3').removeClass('pure-u-1-3');
    };

    self.goToItem = function(item) { 
        self.chosenItemIndex = self.chosenViewData().items.indexOf(item);
        location.hash = self.chosenViewId() + '/' + item.hsaID;

        self.chosenItem = ko.utils.arrayFirst(self.chosenViewData().items, function(itemArr) {
            if (itemArr.hsaID == item.hsaID) {
                return (itemArr);
            }
        });

        // Highlight item and minimize view
        $('.list-item').removeClass('selected');
        $('#' + item.hsaID).addClass('selected');
        $('#view').removeClass('pure-u-2-3').addClass('pure-u-1-3');
    };

    self.saveItem = function(item) {
        console.log('Saving item');
        console.log(item);

        // Find originalItem
        /* var itemOriginal = ko.utils.arrayFirst(self.chosenViewData().items, function(itemArr) {
            if (itemArr.hsaID == item.hsaID) {
                return (itemArr);
            }
        });*/

        // Todo - better replace function
        self.chosenItemData(item);

        // Todo - Do a clone here so we don't have to copy each item
        self.chosenItem.name = item.name;
        self.chosenViewData.valueHasMutated();
    }

    self.cancelSave = function(item) {
        self.chosenItemData(null);
        $('.list-item').removeClass('selected');
        $('#view').removeClass('pure-u-1-3').addClass('pure-u-2-3');
    }

    // Client-side routes    
    Sammy('.container', function() {
        this.get('#:view', function() {
            self.chosenViewId(this.params.view);
            self.chosenItemData(null);

            var dataURL = '/data/' +  this.params.view + '.json';
            
            // $.get(dataURL, params , callback);
            $.get(dataURL, {})
            .done(function( data ) {
                self.chosenViewData(data);
            });
        });

        this.get('#:view/:itemId', function() {
            self.chosenViewId(this.params.view);
            //self.chosenViewData(null);

            var dataURL = '/data/' +  this.params.view + '-' + this.params.itemId + '.json';

            $.get(dataURL, {})
            .done(function(data) {
                self.chosenItemData(data)
            });
        });
    
        this.get('', function() { 
            this.app.runRoute('get', self.config.start) 
        });
    }).run();    
};

ko.applyBindings(new AppVM());

</script>

    </body>
</html>